import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by 蜡笔小新丶 on 2018/4/18.
 */
public class MyJade extends JFrame implements ActionListener {
    private JButton back;
    private JPanel title, content;
    MyPanel myPanel;
    JScrollPane jsb;
    Jade jade;

    public MyJade(User u, ArrayList<Jade> j) {
        back = new JButton("返回首页");
        back.addActionListener(this);
        title = new JPanel();
        content = new JPanel();
        content.setLayout(new FlowLayout());
        content.setPreferredSize(new Dimension(1024, 5000));
        content.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        title.add(back);

        for (int i = 0; i < j.size(); i++) {
            if (j.get(i).getNum() != null && j.get(i).getNum().equals(u.getUserNum())) {
                jade = j.get(i);
                myPanel = new MyPanel(j.get(i),this);
                if(!ExInform.cha(jade.getID())) {
                    myPanel.remove(myPanel.getBuy());
                    JButton no = new JButton("取消上架");
                    no.addActionListener(this);
                    myPanel.add(no);
                }
                myPanel.setPreferredSize(new Dimension(1024 / 5, 340));
                content.add(myPanel);
            }
        }

        jsb = new JScrollPane(content);
        jsb.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsb.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, title, jsb);
        sp.setDividerLocation(40);

        this.setContentPane(sp);
        this.setTitle("我上架的物品");//窗体标签
        this.setSize(1024, 720);//窗体大小
        this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出关闭JFrame
        this.setVisible(true);//显示窗体

        //锁定窗体
        this.setResizable(false);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "返回首页") {
            new Home(ExInform.getU());
            this.dispose();
        }
        if (e.getActionCommand() == "取消上架") {
            JOptionPane ok = new JOptionPane();
            int A = ok.showConfirmDialog(null,"您确定下架该商品吗？","提示消息",JOptionPane.YES_NO_OPTION);
            if(A==0) {
                ExInform.dropJade(jade);
                this.dispose();
                new MyJade(ExInform.getU(), ExInform.show());
            }
        }
    }

    public static void main(String[] args) {
        new MyJade(null, ExInform.show());
    }
}
